#include "tmiceloader.h"
#include "tmglobaldef.h"

TmIceLoader::TmIceLoader(QObject *parent) : QObject(parent)
{
    m_pProcNode = new QProcess();
    m_pProcGlacier2 = new QProcess();
    m_pProcCmd = new QProcess();
    connect(m_pProcCmd, SIGNAL(readyReadStandardOutput()), this, SLOT(slot_proc_cmd_out()));
    connect(m_pProcNode, SIGNAL(readyReadStandardOutput()), this, SLOT(slot_proc_node_out()));
    connect(m_pProcGlacier2, SIGNAL(readyReadStandardOutput()), this, SLOT(slot_proc_glacier2_out()));

    m_pTmTick = new QTimer();
    connect(m_pTmTick, SIGNAL(timeout()), this, SLOT(slot_tm_call()));
}

TmIceLoader::~TmIceLoader()
{
    m_pProcCmd->terminate();
    m_pProcGlacier2->terminate();
    m_pProcNode->terminate();
}

void TmIceLoader::Init()
{
    m_pProcNode->setWorkingDirectory(g_strRootDir);
    m_pProcNode->start("cmd.exe");
    m_pProcGlacier2->setWorkingDirectory(g_strRootDir);
    m_pProcGlacier2->start("cmd.exe");
    m_pProcCmd->setWorkingDirectory(g_strRootDir);
    m_pProcCmd->start("cmd.exe");

    //    m_pProcCmd->write("ipconfig\r\n");

    m_nTmTick = 0;
    m_pTmTick->start(1000);
}

void TmIceLoader::StartNode()
{
    QString strCmdSend = QString("icegridnode --Ice.Config=Config/Ice/node.ini\r\n");
    m_pProcNode->write(g_pCodecASCII->fromUnicode(strCmdSend));
}

void TmIceLoader::AdminAppXml(TmIceLoader::LoadAction a)
{
    QString strCmd;
    switch (a) {
    case LOADER_APP_ADD:
    {
        strCmd = "application add Config/Ice/app.xml";
    }
        break;
    case LOADER_APP_UPDATE:
    {
        strCmd = "application update Config/Ice/app.xml";
    }
        break;
    case LOADER_APP_DEL:
    {
        strCmd = "application remove TamiApp";
    }
        break;
    default:
        break;
    }
    if( strCmd.length()>0 ){
        QString strCmdSend = QString("icegridadmin --Ice.Config=Config/Ice/admin.ini -e \"%1\"\r\n").arg(strCmd);
        m_pProcCmd->write(g_pCodecASCII->fromUnicode(strCmdSend));
    }
}

void TmIceLoader::SendCmd(QString strCmd)
{
    m_pProcCmd->write(g_pCodecASCII->fromUnicode(strCmd));
}

void TmIceLoader::StartGlacier2()
{
    QString strCmdSend = QString("glacier2router --Ice.Config=Config/Ice/glacier2.ini\r\n");
    m_pProcGlacier2->write(g_pCodecASCII->fromUnicode(strCmdSend));
}

void TmIceLoader::StopAll()
{
    m_pProcNode->kill();
    m_pProcGlacier2->kill();
    m_pProcCmd->kill();
    QString strCmdSend = QString("cmd /c %1/closeAll.bat\r\n").arg(g_strRootDir);
    QProcess::startDetached(strCmdSend);
}

void TmIceLoader::slot_tm_call()
{
    m_nTmTick += 1;
    if( m_nTmTick == 1 )
    {
        StartNode();
    }
    if( m_nTmTick == 2 )
    {
        AdminAppXml(LOADER_APP_DEL);
    }
    if( m_nTmTick == 3 )
    {
        AdminAppXml(LOADER_APP_ADD);
    }

    if( m_nTmTick == 7 )
    {
        // Glacier2
        StartGlacier2();
    }
    if( m_nTmTick == 8 ){
        QString strCmdSend = QString("icegridadmin --Ice.Config=Config/Ice/admin.ini -e \"server start ServerScript\"\r\n");
        m_pProcCmd->write(g_pCodecASCII->fromUnicode(strCmdSend));
    }
    if( m_nTmTick == 9 ){
        if( g_bEnableScreenEx ){
            QString strCmdSend = QString("icegridadmin --Ice.Config=Config/Ice/admin.ini -e \"server start ServerScreenEx\"\r\n");
            m_pProcCmd->write(g_pCodecASCII->fromUnicode(strCmdSend));
        }
    }
    if( m_nTmTick == 10 )
    {
        QString strCmdSend = QString("icegridadmin --Ice.Config=Config/Ice/admin.ini -e \"server start ServerNet\"\r\n");
        m_pProcCmd->write(g_pCodecASCII->fromUnicode(strCmdSend));
    }
    if( m_nTmTick > 11 )
    {
        m_pTmTick->stop();
    }
}

void TmIceLoader::slot_proc_cmd_out()
{
    fprintf(stdout, m_pProcCmd->readAll().data());
}

void TmIceLoader::slot_proc_node_out()
{
    fprintf(stdout, m_pProcNode->readAll().data());
}

void TmIceLoader::slot_proc_glacier2_out()
{
    fprintf(stdout, m_pProcGlacier2->readAll().data());
}


